package com.supermap.dataservice.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.user.dao.UserTypeDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.user.UserTypeService;
import com.supermap.dubbo.api.user.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lxs
 * @Title:服务对象 逻辑层
 * @Description:
 * @date 2018/7/26  16:49
 */
@Service
public class UserTypeServiceImpl implements UserTypeService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(UserTypeServiceImpl.class);
    @Resource
    private UserTypeDao userTypeDao;

    @Override
    public JSONUtil getRoleList() {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            List<Role> list = userTypeDao.getRoleList();
            jsonUtil.setMsg("Get getRoleList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
        }catch (Exception e){
        //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getRoleList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getRoleList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
         return jsonUtil;
    }

    @Override
    public JSONUtil getUserTypeList(String areaCode, String code) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            if (areaCode != null) {
                areaCode=areaCode+"%";
            }
            long t1 = System.currentTimeMillis();
            List<UserType> list = userTypeDao.getUserTypeList(areaCode, code);
            long t3 = System.currentTimeMillis();
            System.out.println((t3-t1)/1000);
            jsonUtil.setMsg("Get getUserTypesList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(list);
            long t2 = System.currentTimeMillis();

            System.out.println((t2-t1)/1000);

        }catch (Exception e){
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getUserTypesList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getVipById(String id) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            List<Vip> vip = userTypeDao.getVipById(id);
            jsonUtil.setMsg("Get getVipByuserid success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(vip);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getVipByuserid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getUserById(String id) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            User user = userTypeDao.getUserById(id);
            jsonUtil.setMsg("Get getVipByuserid success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(user);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserTypesList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getVipByuserid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil updateEpathByUserId(String epath, String userId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            Vip vip=userTypeDao.getVipByUserId(userId);//t_vip数据是否存在
            if(vip!=null){//存在
                int updateEpathByUserId=userTypeDao.updateEpathByUserId(epath,userId);//修改t_vip的epath
//                int updateUserRole=userTypeDao.updateUserRole(userId);//修改t_user_role
                if(updateEpathByUserId !=0 ){
                    jsonUtil.setMsg("updateByUserId success！");
                    jsonUtil.setIsSuccess(true);
                    jsonUtil.setCode(200);
                    jsonUtil.setData(updateEpathByUserId);
                }else{
                    jsonUtil.setMsg("updateByUserId flase！");
                    jsonUtil.setIsSuccess(false);
                    jsonUtil.setCode(100);
                }
            }else{
                int insertVip=userTypeDao.insertVip(userId,epath);//添加t_vip
//                int insertUserRole=userTypeDao.insertUserRole(userId);//添加t_user_role
                if(insertVip !=0  ){
                    jsonUtil.setMsg("updateByUserId success！");
                    jsonUtil.setIsSuccess(true);
                    jsonUtil.setCode(200);
                    jsonUtil.setData(insertVip);
                }else{
                    jsonUtil.setMsg("updateByUserId flase！");
                    jsonUtil.setIsSuccess(false);
                    jsonUtil.setCode(100);
                }
            }

        }catch (Exception e){
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>updateByUserId=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("updateByUserId flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getEXPERTListByareaCode(Table table, String areaCode,String subjectId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            List<Exepert> userList = userTypeDao.getEXPERTListByareaCode(table, areaCode,subjectId);
            int count = userTypeDao.getEXPERTCount(areaCode,subjectId);
            table.setCount(count);
            table.setData(userList);
            jsonUtil.setMsg("getUserListByareaCode  success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(table);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("UserTypeServiceImpl=>getUserListByareaCode=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("getUserListByareaCode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
}
